/*
 * This file is part of GuitarMaster.
 * 
 * GuitarMaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GuitarMaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.paris5.master2.guitarmaster.entity;

import com.paris5.master2.guitarmaster.bean.EcomCartRow;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.persistence.*;

/**
 *
 * @author Leticia Mahamed, Alice Khammy, mourad nedjahi, Saliou Mamadou Barry
 */
@Entity
@Table(name = "cart")
public class CartE implements Serializable {
    private static final long serialVersionUID = 1L; 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_cart")
    private int idCart;
    @Column(name="status")
    private Boolean status; 
    
    @Column(name = "amount")
    private Float amount; 
    
    @Column(name = "date_up")
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateUp;
    
    @Column(name = "date_crea")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCrea;
    @JoinColumn(name = "id_user", referencedColumnName = "id_user")
    @ManyToOne(optional=false)
    private User user; 
    @Transient
    private Collection<CartRow> cartRowCollection=new ArrayList<CartRow>();
 
    public CartE() { 
    }

    public int getIdCart() {
        return idCart;
    }

    public void setIdCart(int idCart) {
        this.idCart = idCart;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public float getAmount() {
        return amount;
    }

    public void setAmount(Float amount) {
        this.amount = amount;
    }

    public Date getDateUp() {
        return dateUp;
    }

    public void setDateUp(Date dateUp) {
        this.dateUp = dateUp;
    }

    public Date getDateCrea() {
        return dateCrea;
    }

    public void setDateCrea(Date dateCrea) {
        this.dateCrea = dateCrea;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Collection<CartRow> getCartRowCollection() {
        return cartRowCollection;
    }

    public void setCartRowCollection(Collection<CartRow> cartRowCollection) {
        this.cartRowCollection = cartRowCollection;
    }

    public void addCartRow(CartRow bean)throws EcomException, EcomValidationException{
        if(bean==null){
            throw new EcomValidationException("Empty Cart_Row sent ! (CartRow == null)");
        }
        System.out.println("L'id du produit"+bean.getProduct().getIdProductType());
        if(this.getCartRow(bean.getProduct().getIdProductType())!=null){
            System.out.println("Deja existant");
            throw new EcomException("Product all ready exist in cart");
        }
        System.out.println("Pas existant");
        this.cartRowCollection.add(bean); 
    }
    /**
     * 
     * @param bean
     * @throws EcomValidationException
     * @throws EcomException 
     */
    public void removeCartRow(CartRow bean)throws EcomValidationException,EcomException{
        if(bean==null){
            throw new EcomValidationException("Error, this product is not in the cart. bad request");
        }
          if(this.getCartRow(bean.getProduct().getIdProductType())==null){
            throw new EcomException("Error, this product is not in the cart. bad request");
        }
          this.cartRowCollection.remove(bean);
    }
    /**
     * 
     * @param idProduct
     * @return 
     */
    public CartRow getCartRow(int idProduct) {
        boolean exist = false;
        Iterator<CartRow> cart_rows = this.cartRowCollection.iterator();
        CartRow cartRow = null;
        System.out.println("La taille de la liste"+this.cartRowCollection.size());
        while (cart_rows.hasNext() && !exist) {
            cartRow = cart_rows.next();
            int idProductType = cartRow.getProduct().getIdProductType();
            System.out.println("L'id du produit row"+idProductType+"id du produit"+idProduct);
            if (idProductType == idProduct) {
                System.out.println("Je suis ici");
                exist = true;
            }
        }
        if(!exist){
            cartRow=null;
        }
        return cartRow;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CartE other = (CartE) obj;
        if (this.status != other.status && (this.status == null || !this.status.equals(other.status))) {
            return false;
        }
        if (this.amount != other.amount && (this.amount == null || this.amount!=other.amount)) {
            return false;
        }

        if (this.user != other.user && (this.user == null || !this.user.equals(other.user))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.paris5.master2.guitarmaster.entity.Cart[ cartPK="  +idCart+ " ]";
    }
    
}
